﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace Quality.spc
{
    public partial class XbarR : Form
    {
        DataTable dt;
        public XbarR()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            getdata frm = new getdata();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEventdt += frm_TransfEventdt;
            frm.StartPosition = FormStartPosition.CenterParent;

            frm.Show();
        }
        //事件处理方法
        void frm_TransfEventdt(DataTable value)
        {
            dataGridView1.DataSource = value;
            this.dt = value;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

            double[] v = new double[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                v[i] = Convert.ToDouble(dt.Rows[i][1]);
            }


            int count = dt.Rows.Count;
            double max = SixSigmaHelper.GetArrMax(v);
            double min = SixSigmaHelper.GetArrMin(v);
            int GroupNum = ProcessCababilityHelper.GetGroupNum(dt.Rows.Count);
            int GroupSize = ProcessCababilityHelper.GetGroupSize(count, GroupNum);
            double[] GroupAve = SixSigmaHelper.GetGroupAve(2, GroupSize, v);
            double TotalAveByGroup = SixSigmaHelper.GetTotalAveByGroup(2, GroupSize, v);

            double[] GroupMoveRangeMean = SixSigmaHelper.GetGroupMoveRangeMean(2, v);
            double MoveRangeMean = SixSigmaHelper.GetMoveRangeMean(2, v);
            double TotalStdDev = SixSigmaHelper.GetTotalStdDev(v);
           

            double XUCL = TotalAveByGroup + 0.58F * MoveRangeMean;
            double XCL = TotalAveByGroup;
            double XLCL = TotalAveByGroup - 0.58F * MoveRangeMean;
            double RUCL = 2.11F * MoveRangeMean;
            double RCL = MoveRangeMean;
            double RLCL = 0.0F;

         
            double TARGET=Convert.ToDouble(textBoxTarget.Text);
            double UCL=SixSigmaHelper.GetUCL(TARGET, v);
             double LCL=SixSigmaHelper.GetLCL(TARGET, v);

            double ca = (Math.Abs(TotalAveByGroup - ((max - min) / 2)) / (XUCL - XLCL) / 2);


            double cp = SixSigmaHelper.GetInGroupCP(UCL, LCL, v);
          
            double cpu =SixSigmaHelper.GetInGroupCPU(UCL,v);
            double cpl = SixSigmaHelper.GetInGroupCPL(LCL,v);
            double cpk= SixSigmaHelper.GetInGroupCPK(UCL,LCL,v);
            double pp = SixSigmaHelper.GetPP(UCL, LCL, v);
            double ppu = SixSigmaHelper.GetPPU(UCL, v);
            double ppl = SixSigmaHelper.GetPPL(LCL, v);
            double ppk = SixSigmaHelper.getPPK(UCL, LCL, v);
            labelcount.Text = count.ToString();
            labelgroupnum.Text = GroupNum.ToString();
            labelUSLX.Text = XUCL.ToString("0.00");
            labelCLX.Text = XCL.ToString("0.00");
            labelLSLX.Text = XLCL.ToString("0.00");
            labelUSLR.Text = RUCL.ToString("0.00");
            labelCLR.Text = RCL.ToString("0.00");
            labelLSLR.Text = RLCL.ToString("0.00");
            XBAR.Text = TotalAveByGroup.ToString("0.00");
            RBAR.Text = MoveRangeMean.ToString("0.00");
            labelStdDev.Text = TotalStdDev.ToString("0.00");
             labelCA.Text=ca.ToString("0.00");
             labelCP.Text = cp.ToString("0.00");
             labelCPU.Text = cpu.ToString("0.00");
             labelCPL.Text = cpl.ToString("0.00");
             labelCPK.Text = cpk.ToString("0.00");
             labelPP.Text = pp.ToString("0.00");
             labelPPK.Text = ppk.ToString("0.00");


             //显示Grade等级
             if (cpk < 0.67)
             {
                 labelGrade.Text = "E";
             }
             else
             {
                 if (cpk < 1)
                 {
                     labelGrade.Text = "D";
                 }
                 else
                 {
                     if (cpk < 1.33)
                     {
                         labelGrade.Text = "C";
                     }
                     else
                     {
                         if (cpk < 1.67)
                         {
                             labelGrade.Text = "B";
                         }
                         else
                         {
                             labelGrade.Text = "A";
                         }

                     }

                 }

             }

            drawCurve(GroupAve, chart1, XUCL, XCL, XLCL, GroupNum);
            drawCurve(GroupMoveRangeMean, chart2, RUCL, RCL, RLCL, GroupNum);

        }
        public void drawCurve(double[] data, Chart chart, double UCL, double CL, double LCL, int GroupNum)
        {
            chart.Series.Clear();

            chart.Series.Add("测量值");//添加 
          
            chart.Series.Add("上限");//添加 
          
            chart.Series.Add("下限");//添加 
          
            chart.Series.Add("平均值");//添加 
         
            // 设置均值曲线的样式
            Series series = chart.Series[0];
            // 画均值样条曲线（Spline）
            series.ChartType = SeriesChartType.Spline;
            // 线宽1个像素
            series.BorderWidth = 2;
            // 线的颜色：蓝色
            series.Color = System.Drawing.Color.Blue;
            // 图示上的文字
            series.LegendText = "值曲线";


            // 设置上限曲线的样式
            Series seriesUCL = chart.Series[1];
            // 画上限样条曲线（Spline）
            seriesUCL.ChartType = SeriesChartType.Spline;
            // 线宽1个像素
            seriesUCL.BorderWidth = 1;
            // 线的颜色：红色
            seriesUCL.Color = System.Drawing.Color.Red;
            // 图示上的文字
            seriesUCL.LegendText = "UCL曲线";


            // 设置中限曲线的样式
            Series seriesCL = chart.Series[2];
            // 画中限样条曲线（Spline）
            seriesCL.ChartType = SeriesChartType.Spline;
            // 线宽1个像素
            seriesCL.BorderWidth = 1;
            // 线的颜色：黄色
            seriesCL.Color = System.Drawing.Color.Yellow;
            // 图示上的文字
            seriesCL.LegendText = "CL曲线";


            // 设置下限曲线的样式
            Series seriesLCL = chart.Series[3];
            // 画下限样条曲线（Spline）
            seriesLCL.ChartType = SeriesChartType.Spline;
            // 线宽1个像素
            seriesLCL.BorderWidth = 1;
            // 线的颜色：红色
            seriesLCL.Color = System.Drawing.Color.Red;
            // 图示上的文字
            seriesLCL.LegendText = "LCL曲线";

            //清空曲线
            foreach (var series1 in chart.Series)
            {
                series1.Points.Clear();
            }

            // 在chart中显示均值数据
            int x = 0;
            foreach (float d in data)
            {
                series.Points.AddXY(x, d);
                seriesUCL.Points.AddXY(x, UCL);
                seriesCL.Points.AddXY(x, CL);
                seriesLCL.Points.AddXY(x, LCL);
                x++;
            }
            double MaxAverage = SixSigmaHelper.GetArrMax(data);//最大的R值
            // 设置显示范围
            ChartArea chartArea = chart.ChartAreas[0];
            chartArea.AxisX.Minimum = 0;
            chartArea.AxisX.Maximum = GroupNum - 1;
            chartArea.AxisY.Minimum = (long)(LCL - MaxAverage * 0.5F);
            chartArea.AxisY.Maximum = (long)(UCL + MaxAverage * 0.5F);
            chartArea.BackColor = Color.AliceBlue;
            chartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent;//隐藏X轴网格线
            chartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Transparent;//隐藏Y轴网格线}
        }
    }
    }

